import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {Helper} from "../../common/Helper";
import {HttpProvider} from "../../common/http";
import {UserProvider} from "../../providers/user/user";

@Component({
  selector: 'page-pay-password',
  templateUrl: 'pay-password.html',
})
export class PayPasswordPage {

  private _psw = ''
  get psw() {
    return this._psw
  }

  set psw(val) {
    this._psw = val
    if (val.length == 6) {
      this.pswConfirm = ''
    }
  }

  private _pswConfirm = ''
  get pswConfirm() {
    return this._pswConfirm
  }

  set pswConfirm(val) {
    this._pswConfirm = val
    if (val.length == 6) {
      this.submit()
    }
  }

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private user: UserProvider,
              private helper: Helper,
              private http: HttpProvider) {
  }

  submit() {
    if (this.pswConfirm == this.psw) {
      console.log('submit', this.pswConfirm)
      this.user.setPayPassword({
        data: {
          payPassword: this.pswConfirm
        },
        success: () => {
          this.helper.alert({
            title: '设置成功',
            cssClass: 'g-alert-custom',
            buttons: [{
              text: '确认',
              handler: ()=>{
                this.navCtrl.pop()
              }
            }]
          })
        }
      })
    } else {
      this.http.toastMsg('两次输入的密码不一致')
    }
  }

}
